package com.hhz.time.part2;

import org.junit.Test;

import java.time.*;

/**
 * 时间操作
 *
 * @Author Rem
 * @Date 2020-02-15
 */ 

public class TestLocalDateTime {

    //1. LocalDate LocalTime  LocalDateTime  三者原理一样
    @Test
    public void test() {
        //获取当前日期时间 创建方式1
        LocalDateTime currentTime = LocalDateTime.now();
        System.out.println("当前时间详细:" + currentTime);
        //创建方式2
        LocalDateTime time = LocalDateTime.of(2012, 12, 12, 22, 10, 30);
        System.out.println("创建的时间:" + time);

        int year = currentTime.getYear();
        Month month = currentTime.getMonth();
        int dayOfMonth = currentTime.getDayOfMonth();
        int hour = currentTime.getHour();
        int minute = currentTime.getMinute();
        int second = currentTime.getSecond();
        System.out.println("当前时间-年:" + year);
        System.out.println("当前时间-月:" + month.getValue());
        System.out.println("当前时间-日:" + dayOfMonth);
        System.out.println("当前时间-时:" + hour);
        System.out.println("当前时间-分:" + minute);
        System.out.println("当前时间-秒:" + second);

        System.out.println("------------分割线1-----------");
        System.out.println("当前时间加两年：" + currentTime.plusYears(2));
        System.out.println("当前时间减两个月：" + currentTime.minusMonths(2));


        System.out.println("------------分割线2-----------");
        LocalDateTime nationalDay = currentTime.withMonth(10).withDayOfMonth(1);
        System.out.println("国庆节:" + nationalDay);

        LocalDate MacaoBack = LocalDate.of(1999, Month.SEPTEMBER, 20);
        System.out.println("澳门回归日期:" + MacaoBack);

        LocalTime parse = LocalTime.parse("13:10");
        System.out.println("String转LocalTime:" + parse);


    }

    //2.计算机时间：Instant:时间戳（以 Unix 元年 1970.1.1.00：00:00 到某个时间的毫秒值）
    @Test
    public void test2() {
        Instant now = Instant.now(); //UTC时区
        System.out.println(now);

        //东八区时间
        OffsetDateTime offsetDateTime = now.atOffset(ZoneOffset.ofHours(8));
        System.out.println(offsetDateTime);

        System.out.println("毫秒时间:" + now.toEpochMilli());

        System.out.println("元年+10s:" + Instant.ofEpochSecond(60));
    }


    //Duration:计算两个“时间”之间的间隔
    @Test
    public void test3() throws InterruptedException {
        Instant start = Instant.now();
        Thread.sleep(800);
        Instant end = Instant.now();
        Duration duration = Duration.between(start, end);
        System.out.println("间隔秒：" + duration.toMillis());
        System.out.println("间隔纳秒：" + duration.getNano());

        LocalTime localTime1 = LocalTime.now();
        Thread.sleep(1200);
        LocalTime localTime2 = LocalTime.now();
        System.out.println("间隔秒2：" + Duration.between(localTime1, localTime2).toMillis());

    }

    //Period:计算两个“日期”之间的间隔
    @Test
    public void test4() {
        LocalDate localDate = LocalDate.of(2017, 3, 10);
        LocalDate localDate2 = LocalDate.now();
        Period period = Period.between(localDate, localDate2);
        System.out.println("共间隔月：" + period.toTotalMonths());
        System.out.println("间隔：" + period.getYears() + "年 " + period.getMonths() + "月 " + period.getDays() + "天");

    }
}